import util from '@/js/util/util.js';

let postion = {
    pixelToPercent: function(postion, size) {
        if (postion.unit == '%') {
            return postion.value;
        } else {
            return postion.value * 100 / size;
        }
    },
    percentToPixel: function(postion, size) {
        if (postion.unit == 'px') {
            return postion.value;
        } else {
            return postion.value * size / 100;
        }
    },
    setHorizontalPixel: function(element, size) {// 水平方向设置
        if (util.isBlank(element.style.left.value) && !util.isBlank(element.style.width.value) && !util.isBlank(element.style.right.value)) {
            element.style.left.pixel = util.toInteger(size - this.percentToPixel(element.style.width, size) - this.percentToPixel(element.style.right, size));
            element.style.width.pixel = util.toInteger(this.percentToPixel(element.style.width, size));
        } else if (!util.isBlank(element.style.left.value) && util.isBlank(element.style.width.value) && !util.isBlank(element.style.right.value)) {
            element.style.left.pixel = util.toInteger(this.percentToPixel(element.style.left, size));
            element.style.width.pixel = util.toInteger(size - this.percentToPixel(element.style.left, size) - this.percentToPixel(element.style.right, size));
        } else if (!util.isBlank(element.style.left.value) && !util.isBlank(element.style.width.value) && util.isBlank(element.style.right.value)) {
            element.style.left.pixel = util.toInteger(this.percentToPixel(element.style.left, size));
            element.style.width.pixel = util.toInteger(this.percentToPixel(element.style.width, size));
        }
    },
    setVerticalPixel: function(element, size) {// 垂直方向设置
        if (util.isBlank(element.style.top.value) && !util.isBlank(element.style.height.value) && !util.isBlank(element.style.bottom.value)) {
            element.style.top.pixel = util.toInteger(size - this.percentToPixel(element.style.height, size) - this.percentToPixel(element.style.bottom, size));
            element.style.height.pixel = util.toInteger(this.percentToPixel(element.style.height, size));
        } else if (!util.isBlank(element.style.top.value) && util.isBlank(element.style.height.value) && !util.isBlank(element.style.bottom.value)) {
            element.style.top.pixel = util.toInteger(this.percentToPixel(element.style.top, size));
            element.style.height.pixel = util.toInteger(size - this.percentToPixel(element.style.top, size) - this.percentToPixel(element.style.bottom, size));
        } else if (!util.isBlank(element.style.top.value) && !util.isBlank(element.style.height.value) && util.isBlank(element.style.bottom.value)) {
            element.style.top.pixel = util.toInteger(this.percentToPixel(element.style.top, size));
            element.style.height.pixel = util.toInteger(this.percentToPixel(element.style.height, size));
        }
    },
    setHorizontalValue: function(element, size) {// 水平方向设置
        if (util.isBlank(element.style.left.value) && !util.isBlank(element.style.width.value) && !util.isBlank(element.style.right.value)) {
            if (element.style.width.unit == '%') {
                element.style.width.value = util.toDouble3(element.style.width.pixel * 100 / size);
            } else if (element.style.width.unit == 'px') {
                element.style.width.value = element.style.width.pixel;
            }
            if (element.style.right.unit == '%') {
                element.style.right.value = util.toDouble3((size - element.style.left.pixel - element.style.width.pixel) * 100 / size);
            } else if (element.style.right.unit == 'px') {
                element.style.right.value = size - element.style.left.pixel - element.style.width.pixel;
            }
        } else if (!util.isBlank(element.style.left.value) && util.isBlank(element.style.width.value) && !util.isBlank(element.style.right.value)) {
            if (element.style.left.unit == '%') {
                element.style.left.value = util.toDouble3(element.style.left.pixel * 100 / size);
            } else if (element.style.left.unit == 'px') {
                element.style.left.value = element.style.left.pixel;
            }
            if (element.style.right.unit == '%') {
                element.style.right.value = util.toDouble3((size - element.style.left.pixel - element.style.width.pixel) * 100 / size);
            } else if (element.style.right.unit == 'px') {
                element.style.right.value = size - element.style.left.pixel - element.style.width.pixel;
            }
        } else if (!util.isBlank(element.style.left.value) && !util.isBlank(element.style.width.value) && util.isBlank(element.style.right.value)) {
            if (element.style.left.unit == '%') {
                element.style.left.value = util.toDouble3(element.style.left.pixel * 100 / size);
            } else if (element.style.left.unit == 'px') {
                element.style.left.value = element.style.left.pixel;
            }
            if (element.style.width.unit == '%') {
                element.style.width.value = util.toDouble3(element.style.width.pixel * 100 / size);
            } else if (element.style.width.unit == 'px') {
                element.style.width.value = element.style.width.pixel;
            }
        }
    },
    setVerticalValue: function(element, size) {// 垂直方向设置
        if (util.isBlank(element.style.top.value) && !util.isBlank(element.style.height.value) && !util.isBlank(element.style.bottom.value)) {
            if (element.style.height.unit == '%') {
                element.style.height.value = util.toDouble3(element.style.height.pixel * 100 / size);
            } else if (element.style.height.unit == 'px') {
                element.style.height.value = element.style.height.pixel;
            }
            if (element.style.bottom.unit == '%') {
                element.style.bottom.value = util.toDouble3((size - element.style.top.pixel - element.style.height.pixel) * 100 / size);
            } else if (element.style.bottom.unit == 'px') {
                element.style.bottom.value = size - element.style.top.pixel - element.style.height.pixel;
            }
        } else if (!util.isBlank(element.style.top.value) && util.isBlank(element.style.height.value) && !util.isBlank(element.style.bottom.value)) {
            if (element.style.top.unit == '%') {
                element.style.top.value = util.toDouble3(element.style.top.pixel * 100 / size);
            } else if (element.style.top.unit == 'px') {
                element.style.top.value = element.style.top.pixel;
            }
            if (element.style.bottom.unit == '%') {
                element.style.bottom.value = util.toDouble3((size - element.style.top.pixel - element.style.height.pixel) * 100 / size);
            } else if (element.style.bottom.unit == 'px') {
                element.style.bottom.value = size - element.style.top.pixel - element.style.height.pixel;
            }
        } else if (!util.isBlank(element.style.top.value) && !util.isBlank(element.style.height.value) && util.isBlank(element.style.bottom.value)) {
            if (element.style.top.unit == '%') {
                element.style.top.value = util.toDouble3(element.style.top.pixel * 100 / size);
            } else if (element.style.top.unit == 'px') {
                element.style.top.value = element.style.top.pixel;
            }
            if (element.style.height.unit == '%') {
                element.style.height.value = util.toDouble3(element.style.height.pixel * 100 / size);
            } else if (element.style.height.unit == 'px') {
                element.style.height.value = element.style.height.pixel;
            }
        }
    },
    setElementPostion: function(elementList, width, height) {
        for (let i = 0; i < elementList.length; i ++) {
            this.setHorizontalPixel(elementList[i], width);
            this.setVerticalPixel(elementList[i], height);
        }
    }
};

export default postion;